/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BD;

import BE.Cliente;
import java.util.Collection;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Xatuuu
 */
public class ClienteBD {

    private Session session = null;

    public ClienteBD() {

        this.session = HibernateUtil.getSessionFactory().getCurrentSession();

    }

    public Collection buscarMesaenCliente(Cliente objCliente) {
        List<Cliente> lista = null;
        try {
            Transaction tx = session.beginTransaction();
            Criteria crit = session.createCriteria(Cliente.class);

            if (objCliente.getSrlIdpersona() > 0) {
                crit.add(Restrictions.eq("srlIdpersona", objCliente.getSrlIdpersona()));
            }
            if (objCliente.getMesa() != null) {
                if (objCliente.getMesa().getSrlIdmesa() > 0) {
                    crit.add(Restrictions.eq("mesa.srlIdmesa", objCliente.getMesa().getSrlIdmesa()));
                }
                     if (objCliente.getMesa().getCvNombre() != "") {
               crit.add(Restrictions.like("mesa.cvNombre","%" + objCliente.getMesa().getCvNombre()+ "%" ));
                }
            }
             crit.add(Restrictions.eq("boolEliminado", false));
            session.flush();
            lista = crit.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }



    public boolean modificarCliente(Cliente objcliente) {
        try {

            Transaction tx = session.beginTransaction();
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = (Transaction) session.beginTransaction();
            
            session.evict(objcliente);

            Cliente objClienteX = (Cliente) session.get(Cliente.class, new Integer(objcliente.getSrlIdpersona()));


            session.saveOrUpdate(objClienteX);

            tx.commit();
            //session.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Collection seleccionarCliente() {

        Transaction tx = session.beginTransaction();
        Criteria crit = session.createCriteria(Cliente.class);
        return crit.list();
    }
}
